import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
/**
 * @author Andik Taufiq
 * 2010, September 19
 */
public class Mockup8Puzzle extends MIDlet implements Runnable {
    Display display;
    private MainCanvas mainCanvas;

    private boolean isInitialized;
    private boolean splashIsShown;
    Thread splashSimple;

    public Image splash;
    public Image logo;
    public boolean isLoadSuccess = false;

    public Mockup8Puzzle() {
        display = Display.getDisplay(this);
        try {
            splash		= Image.createImage("/splashback.jpg");
            logo		= Image.createImage("/splashlogo.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mainCanvas = new MainCanvas(this, display);
    }

    public void startApp() {
        splashSimple = new Thread(new Mockup8Puzzle.SplashScreen());
        splashSimple.start();

        Thread appThread = new Thread(this);
        appThread.start();
    }

    public void run(){
        while(!splashIsShown){
            Thread.yield();
        }
        doTimeConsumingInit();
        while(true){
            Thread.yield();
        }
    }

    private void doTimeConsumingInit(){
        //long endTime= System.currentTimeMillis()+5000;
        //while(System.currentTimeMillis()<endTime ){}
        while (isLoadSuccess == false) {}

        isInitialized = true;
        display.setCurrent(mainCanvas);
        splashSimple = null;
        splash = null;
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void exitMIDlet() {
        destroyApp(false);
        notifyDestroyed();
    }

    public class SplashScreen implements Runnable{
        public SplashCanvas splashCanvas;

        public void run(){
            splashCanvas = new SplashCanvas();
            display.setCurrent(splashCanvas);
            splashCanvas.repaint();
            splashCanvas.serviceRepaints();
            while(!isInitialized){
                try{
                    Thread.yield();
                }catch(Exception e){}
            }
        }
    }

    public class SplashCanvas extends Canvas implements Runnable {

        private int barWidth = 0;

        public int barSize = 196;

        public void run() {
            if (barWidth < 196)	{
                barWidth = barWidth + 1;
            } else {
                isLoadSuccess = true;
            }
            repaint();
        }

        protected void paint(Graphics g){
            setFullScreenMode(true);
            if ((splash != null) && (logo != null)) {
                g.drawImage(splash, 0, 0, Graphics.TOP | Graphics.LEFT);
                g.drawImage(logo, getWidth()/2, 170, Graphics.TOP | Graphics.HCENTER);
            }
            g.setColor(0x3F73A4);
            g.drawRect(20,300,200,4);
            g.setColor(0xB3D2EF);
            g.fillRect(22,302,barWidth,1);
            splashIsShown = true;
            display.callSerially(this);
        }
    }
}
